
/*
 *
 *  * Copyright (c) 2016. For Intelligent Group.
 *
 */

package com.intelligent.ispc.chart.service.impl;

import com.intelligent.ispc.chart.grr.APlotChart;
import com.intelligent.ispc.chart.grr.ComponentsChart;
import com.intelligent.ispc.chart.grr.RPlotChart;
import com.intelligent.ispc.chart.grr.XBarOrRChart;
import com.intelligent.ispc.chart.service.ChartExporter;
import com.intelligent.ispc.chart.spc.ControlChart;
import com.intelligent.ispc.chart.spc.NormalDistributionChart;
import com.intelligent.ispc.chart.spc.ScatterChart;
import com.intelligent.ispc.common.utils.ApplicationException;
import com.intelligent.ispc.constant.AppConstant;
import com.intelligent.ispc.core.dto.ChartDto;
import com.intelligent.ispc.core.dto.GrrChartDto;
import com.intelligent.ispc.core.dto.GrrExportParamDto;
import com.intelligent.ispc.core.dto.GrrResultDto;
import com.intelligent.ispc.r.dto.ControlDataDto;
import com.intelligent.ispc.r.dto.NormalDistributionDto;
import com.intelligent.ispc.r.dto.ScatterDto;
import org.jfree.chart.JFreeChart;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.imageio.ImageIO;
import java.io.*;
import java.util.List;
import java.util.Map;

/**
 * Created by Can.Guan on 2016/9/7.
 */
public class ChartExporterImpl implements ChartExporter {
    private static Logger logger = LoggerFactory.getLogger(ChartExporterImpl.class);

    /*
        precision number
     */
    private int digNum;

    /**
     * empty constructor
     */
    public ChartExporterImpl() {

    }

    /**
     * constructor
     *
     * @param digNum precision number
     */
    public ChartExporterImpl(int digNum) {
        this.digNum = digNum;
    }

    @Override
    public void exportSpcScatter2Gif(ScatterDto scatterDto, String charName, String path) {
        ScatterChart chart = new ScatterChart(scatterDto, charName);
        chart.initDigNumFormat(digNum);
        saveImg(chart.createChartPanel().getChart(), path);
    }

    @Override
    public void exportSpcNDChart2Gif(NormalDistributionDto normalDistributionDto, String chartName, String path) {
        NormalDistributionChart chart = new NormalDistributionChart(chartName, normalDistributionDto);
        chart.initDigNumFormat(digNum);
        saveImg(chart.createChartPanel().getChart(), path);
    }

    @Override
    public void exportSpcRunChart2Gif(ControlDataDto controlDataDto, ChartDto chartDto, String path) {
        ControlChart chart = new ControlChart(controlDataDto, chartDto);
        chart.initDigNumFormat(digNum);
        saveImg(chart.createChartPanel().getChart(), path);
    }

    @Override
    public void exportSpcAllRunChart2Gif(Map<String, ControlDataDto> controlDataDtoMap, ChartDto chartDto, Map<String, String> imgPath) {
        for (String i : controlDataDtoMap.keySet()) {
            exportSpcRunChart2Gif(controlDataDtoMap.get(i), chartDto, imgPath.get(i));
        }
    }

    @Override
    public void exportGrrAPlotChart2Gif(double[][] aplotValue, int operator, int part, String path) {
        APlotChart chart = new APlotChart(aplotValue, operator, part);
        chart.initDigNumFormat(digNum);
        saveImg(chart.createChartPanel().getChart(), path);
    }

    @Override
    public void exportGrrComponentsChart2Gif(double[][] componentData, String path) {
        ComponentsChart chart = new ComponentsChart(componentData);
        chart.initDigNumFormat(digNum);
        saveImg(chart.createChartPanel().getChart(), path);
    }

    @Override
    public void exportGrrRPlotChart2Gif(double[] value, String chartType, int part, int trial, String path) {
        RPlotChart chart = new RPlotChart(value, chartType, part, trial);
        chart.initDigNumFormat(digNum);
        saveImg(chart.createChartPanel().getChart(), path);
    }

    @Override
    public void exportGrrXBarChart2Gif(double[] value, int part, String path) {
        String chartType = "X-Bar Chart";
        XBarOrRChart chart = new XBarOrRChart(value, chartType, part);
        chart.initDigNumFormat(digNum);
        saveImg(chart.createChartPanel().getChart(), path);
    }

    @Override
    public void exportGrrRChart2Gif(double[] value, int part, String path) {
        String chartType = "R Chart";
        XBarOrRChart chart = new XBarOrRChart(value, chartType, part);
        chart.initDigNumFormat(digNum);
        saveImg(chart.createChartPanel().getChart(), path);
    }

    @Override
    public void exportGrrChart(GrrResultDto grrResultDto, GrrExportParamDto grrExportParamDto, List<String> imgPaths) {
        int part;
        int trail;
        int operator;
        part = Integer.parseInt(grrExportParamDto.getSnCount());
        trail = Integer.parseInt(grrExportParamDto.getTrialCount());
        operator = Integer.parseInt(grrExportParamDto.getOperatorCount());
        if (grrResultDto != null) {
            GrrChartDto grrChartDto = grrResultDto.getGrrChartDto();
            if (grrChartDto != null) {
                final int num0 = 0, num1 = 1, num2 = 2, num3 = 3, num4 = 4, num5 = 5;
                exportGrrXBarChart2Gif(grrChartDto.getXbarValue(), part, imgPaths.get(num0));
                exportGrrRChart2Gif(grrChartDto.getrValue(), part, imgPaths.get(num1));
                exportGrrRPlotChart2Gif(grrChartDto.getResByOperatorValue(), "R&R Plot – By Appraisers", part, trail, imgPaths.get(num2));
                exportGrrRPlotChart2Gif(grrChartDto.getResByPartValue(), "R&R Plot – By Parts", operator, trail, imgPaths.get(num3));
                exportGrrComponentsChart2Gif(grrChartDto.getComponentValue(), imgPaths.get(num4));
                exportGrrAPlotChart2Gif(grrChartDto.getAplotValue(), operator, part, imgPaths.get(num5));
            }
        }
    }

    /*
        private method to saving graph to gif format
     */
    private void saveImg(JFreeChart chart, String path) {
        int width = AppConstant.EXPORT_IMG_WEIGHT;
        int height = AppConstant.EXPORT_IMG_HEIGHT;
        try {
            logger.debug("Saving image : " + path);
            File file = new File(path);
            ImageIO.write(chart.createBufferedImage(width, height), "gif", file);
            logger.debug("Save done : " + path);
        } catch (Exception e) {
            logger.error(e.getMessage());
            throw new ApplicationException(e);
        }
    }
}

